package com.byit.async;

import com.alibaba.fastjson.JSONObject;
import com.byit.EncryptType.MD5;
import com.byit.domain.AppUser;
import com.byit.dx.SendMessService;
import com.byit.jgpush.JgPushExtra;
import com.byit.jgpush.JgPushService;
import com.byit.service.IAppUserService;
import com.byit.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证时异步发送推送消息
 *
 * @author tjm
 * @date 2019/1/10.
 */
@Component
@Slf4j
public class AuthPushAsync {

    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private JgPushService jgPushService;
    /**
     * 发送推送和短信
     * @param userId
     * @param status
     */
    @Async
    public void sendMsg(Long userId,Integer status,int type){
        try {
            String obj = "个人";
            if (1==type){
                obj = "企业";
            }
            AppUser appUser = appUserService.selectById(userId);
            //发送短信
            String msg;
            if (status == 1){
                //如果，状态改为审核中，不发通知信息
                return;
            }
            if (status == 2){
                msg =  "尊敬的用户：恭喜您的"+obj+"认证信息审核通过！";
            }else {
                msg = "尊敬的用户：对不起，您的"+obj+"认证审核没有通过，请登录app查看具体原因。";
            }
            //发送短信
            SendMessService.sendSms(msg,appUser.getPhone());
            //极光推送
            if (StringUtil.isNotEmpty(appUser.getDeviceId())) {
                JSONObject param = new JSONObject();
                param.put("alias", MD5.md5(appUser.getDeviceId()+":"+userId));
                param.put("msg", msg);
                param.put("content", msg);
                param.put("title", "认证通知");
                JgPushExtra extra = new JgPushExtra();
                extra.setType(5);
                extra.setLevel(2);
                extra.setContent(msg);
                extra.setTitle("认证通知");
                extra.setTime(new Date());
                extra.setSource("智慧江油");
                param.put("data", "extra");
                //发送自定义推送
                jgPushService.jpushAndroidIOSByaliasForMessage(param);
                //发送通知
                jgPushService.jpushAndroidIOSByAlias(param);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }
}
